// 文件名：week1\problem\lc643_欧琳琳.cpp
// 提交链接：https://leetcode.cn/problems/maximum-average-subarray-i/submissions/559475432?envType=problem-list-v2&envId=sliding-window&status=SOLVED
// 643. 数组，滑动窗口
// 完成日期：2024/08/29
// c++,使用滑动窗口解决，end和start两指针动态调整窗口大小

class Solution {
public:
    double findMaxAverage(vector<int>& nums, int k) {
        int n=nums.size();
        int start=0;
        double sum=0;
        double maxsum=-DBL_MAX;;
        for(int end=0;end<n;end++)
        {
            sum+=nums[end];
            while(end-start+1==k)
            {
                maxsum=max(maxsum,sum);
                sum-=nums[start];
                start++;
            }
        }
        return maxsum/k;
    }
};